<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="/WEB-INF/jsp/include/head.jsp"/>
    <style type="text/css">
        .ztree li span.button.add {
            margin-left: 2px;
            margin-right: -1px;
            background-position: -144px 0;
            vertical-align: top;
            *vertical-align: middle
        }
    </style>
</head>
<body>

<!-- 面包屑导航 -->
<ul class="breadcrumb">
    <li><strong>当前位置： </strong>基础设置</li>
    <li class="active">专业设置</li>
</ul>


<div class="panel panel-info">
    <div class="panel-heading">
        <strong>专业设置</strong>

        <a role="button" id="btn_save_data" style="float: right;margin-left: 10px" class="btn btn-default btn-xs btn-success"
           href="javaScript:;"><span class="glyphicon glyphicon-ok"></span> 保存 </a>

        <a role="button" id="btn_refresh" style="float: right;margin-left: 10px" class="btn btn-default btn-xs btn-success"
           href="javaScript:;"><span class="glyphicon glyphicon-refresh"></span> 刷新 </a>


    </div>
    <div class="panel-body" style=" overflow:auto;  max-height:400px; padding-left: 100px">
        <ul id="tree_data" class="ztree"></ul>
    </div>
</div>


<div class="modal fade" id="modal_ajax">
    <div class="modal-dialog  modal-mid ">
        <div class="modal-content ">
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<jsp:include page="/WEB-INF/jsp/include/footer.jsp"/>
<SCRIPT type="text/javascript">
    var setting = {
        async: {
            enable: true,
            url: "dictionary/majorType/select"
        },
        check: {
            enable: true
        },
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: function (treeId, treeNode) {
                $("#addBtn_" + treeNode.tId).unbind().remove();
                $("#editBtn_" + treeNode.tId).unbind().remove();
                $("#toolSpan_" + treeNode.tId).remove();
            },
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true
            }
        }, callback: {
            onAsyncSuccess: function (event, treeId, treeNode, msg) {
                var treeObj = $.fn.zTree.getZTreeObj(treeId);
                $.each(treeObj.getNodes(), function (i, v) {
                    treeObj.expandNode(v, true, false, true);
                })
            }
        }
    };

    function addHoverDom(treeId, treeNode) {
        if ($("#toolSpan_" + treeNode.tId).length > 0) return;

        console.log(treeNode);
        var toolSpan =
                "<span id='toolSpan_" + treeNode.tId + "'>"
                + (treeNode.level<3 ?  "<span  style='margin: 0 5px' class='button add'  id='addBtn_" + treeNode.tId + "' title='添加子节点' onfocus='this.blur();'></span>":"" )
                + "<span  style='margin: 0 5px' class='button edit' id='editBtn_" + treeNode.tId + "' title='修改该项'   onfocus='this.blur();'></span>"
                + "</span>";
        $("#" + treeNode.tId + "_span").after(toolSpan);
        var addBtn = $("#addBtn_" + treeNode.tId);
        if (addBtn) {
            addBtn.bind("click", function () {
                $('#modal_ajax').modal({
                    backdrop: 'static',
                    keyboard: false,
                    show: true,
                    remote: 'dictionary/majorType/editDlg?pid=' + treeNode.id
                });
            });
        }
        var editBtn = $("#editBtn_" + treeNode.tId);
        if (editBtn) {
            editBtn.bind("click", function () {
                $('#modal_ajax').modal({
                    backdrop: 'static',
                    keyboard: false,
                    show: true,
                    remote: 'dictionary/majorType/editDlg?id=' + treeNode.id
                });
            });
        }
    }
    ;


    $("#btn_save_data").click(function () {
        var treeObj = $.fn.zTree.getZTreeObj("tree_data");
        var nodes = treeObj.getChangeCheckedNodes(), data = {checkNodes: [], noCheckNodes: []};

        console.log(nodes);
        $.each(nodes, function (i, v) {
            if (v.checked) {
                data.checkNodes.push(v.id);
            } else {
                data.noCheckNodes.push(v.id);
            }
        });

        if (data.checkNodes.length + data.noCheckNodes.length == 0) {
            swal("", "未发现数据被修改");
            return;
        }

        $.sysPlugin.superAjax({
            data: data,
            url: 'dictionary/majorType/updateCheck',
            operateType: 'update',
            showSuccessAlert: true,
            showWarnAlert: true,
            successCall: function (data) {
                $.sysPlugin.successCallBack(data, {
                    zTreeId: 'tree_data'
                });
            }
        });
    });




    $(function () {
        $.fn.zTree.init($("#tree_data"), setting);
    });

    $("#btn_refresh").click(function(){
        var treeObj = $.fn.zTree.getZTreeObj("tree_data");
        if (treeObj.setting.async.enable == true) {
            treeObj.reAsyncChildNodes(null, "refresh");
        } else {
            treeObj.refresh();
        }
    });

    $.sysPlugin.clearModalRemoveData("modal_ajax");

</SCRIPT>


</body>
</html>